import {reqAddShopCart,reqChangeIsChecked,reqShopCartListInfo,reqChangeAllIsChecked,reqDeleteOne,reqDeleteAll} from '@/api'
const state = {
    shopCartList:[]
};
const mutations = {
    REQ_SHOPCARTLISTINFO(state,shopCartList){
        state.shopCartList = shopCartList;
    }
};
const actions = {
    async addShopCart({commit},{skuId,skuNum }){
        const result = await reqAddShopCart(skuId,skuNum )
        // 代表添加购物车成功
        if(result.code === 200){
            return 'ok'
        }else{
            return Promise.reject(new Error('failed to add shop'))
        }
    },
    async getShopCartListInfo({commit}){
        const result = await reqShopCartListInfo()
        if(result.code === 200){
            commit('REQ_SHOPCARTLISTINFO',result.data)
        }
    },
    // 请求修改购物车商品的选中状态
    async changeIsChecked({commit},{skuId,isChecked}){
        const result = await reqChangeIsChecked(skuId,isChecked)
        if(result.code === 200){
            return 'ok'
        }else{
            return Promise.reject(new Error('failed'))
        }
    },
    // 请求批量修改购物车中商品的选中状态
    async changeAllIsChecked({commit},{isChecked,skuIdList}){
        const result = await reqChangeAllIsChecked(isChecked,skuIdList);
        if(result.code === 200){
            return 'ok'
        }else{
            return Promise.reject(new Error('failed'))
        }
    },
    // 请求删除单个购物车商品信息
    async deleteOne({commit},skuId){
        const result = await reqDeleteOne(skuId)
        if(result.code === 200){
            return 'ok'
        }else{
            return Promise.reject(new Error('failed'))
        }
    },
    // 请求删除全部选中商品信息
    async deleteAll({commit},skuIdList){
        const result = await reqDeleteAll(skuIdList)
        if(result.code === 200){
            return 'ok'
        }else{
            return Promise.reject(new Error('failed'))
        }
    }
};
const getters = {};

export default {
    state,
    actions,
    mutations,
    getters,
}